package com.qububu.play.common.control;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.common.user.dto.UserInfoDto;
import com.qububu.play.common.dto.ActionTalkoverDto;
import com.qububu.play.common.dto.ActionTalkoverDto.Type;
import com.qububu.play.common.dto.TalkBaseMessage;
import com.qububu.play.common.service.TalkService;
import com.qububu.utils.InitScriptSession;
import static com.qububu.utils.ParamConverter.assertNotEmpty;
import com.qububu.utils.SystemConstants;

@Controller
@RequestMapping("/play/talk")
@RemoteProxy(name="dwrService")
public class TalkControl {

	@Resource
	private TalkService talkService;
	
	@RequestMapping("/chat")
	public String toChat(){
		
		return "chat";
	}
	
	@RequestMapping("/getChatAuoutMessages")
	@ResponseBody
	public Map<String, Object> getChatAuoutMessages(HttpServletRequest req){
		int ONE_PAGE_DATA_COUNT = 20;
		int searchLimitBegin = 0;
		int searchLimitEnd = (searchLimitBegin + 1) * ONE_PAGE_DATA_COUNT;
		String actionType = assertNotEmpty(req.getParameter("actionType"));
		String actionId = assertNotEmpty(req.getParameter("actionId"));
		// 只显示聊天记录的一部分
		List<ActionTalkoverDto> talkList = talkService.getTalkAboutMessages(Type.getType(actionType).toIntValue(), actionId, searchLimitBegin, searchLimitEnd);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("messages", talkList);
		
		return resultMap;
	}
	
	@RequestMapping("/getChatMessages")
	@ResponseBody
	public Map<String, Object> getChatMessages(HttpServletRequest req){
		String actionType = assertNotEmpty(req.getParameter("actionType"));
//		actionType = "DINE";
		String actionId = assertNotEmpty(req.getParameter("actionId"));
		// 分页显示
		List<ActionTalkoverDto> talkList = talkService.getTalkMessages(Type.getType(actionType).toIntValue(), actionId);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("messages", talkList);
		
		return resultMap;
	}
	
	@RemoteMethod
	public void onload(String actionId, String typeName){
		actionId = assertNotEmpty(actionId);
		typeName = assertNotEmpty(typeName);
		if (WebContextFactory.get().getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME) != null) {
			UserInfoDto user = (UserInfoDto) WebContextFactory.get().getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);
			ScriptSession scriptSession = WebContextFactory.get().getScriptSession();
			TalkBaseMessage baseMessage = new TalkBaseMessage();
			baseMessage.setActionId(actionId);
			baseMessage.setNickName(user.getNickName());
			baseMessage.setUserId(user.getUserId());
			baseMessage.setActionType(Type.getType(typeName));
			scriptSession.setAttribute("talkBaseMessage", baseMessage);
			InitScriptSession initScriptSession = new InitScriptSession();
			try{
				initScriptSession.init();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
}
